package com.ssy.lingxi.member.merchant.serviceimpl.web;

import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.config.ServiceConfig;
import com.ssy.lingxi.member.merchant.model.vo.basic.request.MemberTypeIdVO;
import com.ssy.lingxi.member.merchant.model.vo.basic.request.RoleIdVO;
import com.ssy.lingxi.member.merchant.model.vo.basic.request.UpperMemberIdRoleIdVO;
import com.ssy.lingxi.member.merchant.model.vo.basic.response.MemberTypeIdAndNameVO;
import com.ssy.lingxi.member.merchant.model.vo.basic.response.UpperMemberShowVO;
import com.ssy.lingxi.member.merchant.model.vo.info.request.*;
import com.ssy.lingxi.member.merchant.model.vo.info.response.*;
import com.ssy.lingxi.member.merchant.model.vo.lifecycle.response.MemberAppraisalPageQueryVO;
import com.ssy.lingxi.member.merchant.model.vo.lifecycle.response.MemberCreditComplaintPageQueryVO;
import com.ssy.lingxi.member.merchant.model.vo.lifecycle.response.MemberRecordRectifyVO;
import com.ssy.lingxi.member.merchant.model.vo.maintenance.request.MemberDetailCreditHistoryPageVO;
import com.ssy.lingxi.member.merchant.model.vo.maintenance.response.*;
import com.ssy.lingxi.member.merchant.model.vo.validate.request.ValidateIdPageVO;
import com.ssy.lingxi.member.merchant.model.vo.validate.request.ValidateIdVO;
import com.ssy.lingxi.member.merchant.model.vo.validate.response.MemberValidateDetailLevelVO;
import com.ssy.lingxi.member.merchant.service.base.IBaseMemberCacheService;
import com.ssy.lingxi.member.merchant.service.base.IBaseMemberInfoService;
import com.ssy.lingxi.member.merchant.service.base.IBaseMemberInnerService;
import com.ssy.lingxi.member.merchant.service.base.IBaseSiteService;
import com.ssy.lingxi.member.merchant.service.web.IMemberAbilityInfoService;
import com.ssy.lingxi.member.merchant.service.web.IPlatformMemberRoleRuleService;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 会员信息管理查询服务接口
 * @author 万宁
 * @version 2.0.0
 * @date 2020-09-05
 */
@Service
public class MemberAbilityInfoServiceImpl implements IMemberAbilityInfoService {
    @Resource
    private IBaseMemberCacheService memberCacheService;

    @Resource
    private IBaseMemberInfoService baseMemberInfoService;

    @Resource
    private IBaseMemberInnerService baseMemberInnerService;

    @Resource
    private IBaseSiteService siteService;

    @Resource
    private IPlatformMemberRoleRuleService roleRuleService;

    /**
     * 获取分页查询页面下拉框内容
     *
     * @param headers Http头部信息
     * @return 下拉框内容
     */
    @Override
    public Wrapper<MemberInfoSearchConditionVO> getPageSearchConditions(HttpHeaders headers) {
        memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getPageSearchConditions();
    }

    /**
     * 分页、模糊查询归属会员列表
     * @param headers Http头部信息
     * @param pageVO  接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<PageData<UpperMemberInfoVO>> pageUpperMembers(HttpHeaders headers, MemberInfoPageVO pageVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        Wrapper<Boolean> wrapper = siteService.isEnableMultiTenancy(headers);
        return baseMemberInfoService.pageUpperMembers(loginUser.getMemberId(), loginUser.getMemberRoleId(), pageVO.getName(), pageVO.getStartDate(),pageVO.getEndDate(),pageVO.getOuterStatus(), pageVO.getCurrent(), pageVO.getPageSize(), wrapper.getData());
    }

    /**
     * “申请会员”页面，查询按钮状态和文本
     *
     * @param headers Http头部信息
     * @param conditionVO    接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberInfoApplyButtonVO> getApplyCondition(HttpHeaders headers, MemberApplyConditionVO conditionVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getApplyCondition(loginUser, conditionVO.getShopType(), conditionVO.getUpperMemberId(), conditionVO.getUpperRoleId());
    }

    /**
     * “邀请会员”页面，查询按钮状态和文本
     *
     * @param headers     Http头部信息
     * @param conditionVO 接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberInfoInviteButtonVO> getInviteCondition(HttpHeaders headers, MemberInviteConditionVO conditionVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getInviteCondition(loginUser, conditionVO.getShopType(), conditionVO.getSubMemberId(), conditionVO.getSubRoleId());
    }

    /**
     * “申请会员”页面，会员注册资料信息
     *
     * @param headers Http头部信息
     * @param idVO    接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberInfoApplyRegisterDetailVO> getApplyRegisterDetail(HttpHeaders headers, UpperMemberIdRoleIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getApplyRegisterDetail(loginUser, idVO.getUpperMemberId(), idVO.getUpperRoleId());
    }

    /**
     * “申请会员”页面，会员入库资料信息
     *
     * @param headers Http头部信息
     * @param idVO    接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberInfoApplyDepositDetailVO> getApplyDepositDetail(HttpHeaders headers, UpperMemberIdRoleIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getApplyDepositDetail(loginUser, idVO.getUpperMemberId(), idVO.getUpperRoleId());
    }

    /**
     * “申请会员” - 提交
     *
     * @param headers Http头部信息
     * @param subVO   接口参数
     * @return 申请结果
     */
    @Override
    public Wrapper<Void> applyToBeSubMember(HttpHeaders headers, MemberInfoApplyForSubVO subVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInnerService.applyToBeSubMember(subVO.getUpperMemberId(), subVO.getUpperRoleId(), loginUser.getMemberId(), loginUser.getMemberRoleId(), subVO.getDepositDetails(), subVO.getQualities());
    }

    /**
     * 获取“修改会员信息”页面，会员注册资料信息
     *
     * @param headers    Http头部信息
     * @param idVO 接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberInfoUpdateDetailVO> getMemberRegisterDetail(HttpHeaders headers, ValidateIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getMemberRegisterDetail(loginUser, idVO.getValidateId());
    }

    /**
     * 修改会员注册信息
     * @param headers  Http头部信息
     * @param detailVO 接口参数
     * @return 修改结果
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public Wrapper<Void> updateMemberRegisterDetail(HttpHeaders headers, MemberInfoUpdateRegisterDetailVO detailVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.updateMemberRegisterDetail(loginUser, detailVO);
    }

    /**
     * 获取“修改入库信息”页面，会员入库资料信息
     *
     * @param headers Http头部信息
     * @param idVO    会员关系Id
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberInfoUpdateDepositDetailQueryVO> getMemberDepositDetail(HttpHeaders headers, ValidateIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getMemberDepositDetail(loginUser, idVO.getValidateId());
    }

    /**
     * 修改会员入库信息
     *
     * @param headers  Http头部信息
     * @param detailVO 接口参数
     * @return 修改结果
     */
    @Override
    public Wrapper<Void> updateMemberDepositDetail(HttpHeaders headers, MemberInfoUpdateDepositDetailVO detailVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.updateMemberDepositDetail(loginUser, detailVO);
    }

    /**
     * 会员详情 - 会员基本信息
     *
     * @param headers    Http头部信息
     * @param idVO 接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberInfoBasicDetailVO> getMemberBasicDetail(HttpHeaders headers, ValidateIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getMemberBasicDetail(loginUser, idVO.getValidateId());
    }

    /**
     * 会员详情 - 会员档案信息
     *
     * @param headers Http头部信息
     * @param idVO    接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberInfoDepositDetailVO> getMemberArchives(HttpHeaders headers, ValidateIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getMemberArchives(loginUser, idVO.getValidateId());
    }

    /**
     * 会员详情- 会员档案 - 分页查询考评信息
     *
     * @param headers HttpHeader信息
     * @param pageVO  接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<PageData<MemberAppraisalPageQueryVO>> pageMemberAppraisal(HttpHeaders headers, ValidateIdPageVO pageVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.pageMemberAppraisal(loginUser, pageVO);
    }

    /**
     * 会员详情 - 会员档案 - 分页查询整改信息
     *
     * @param headers Http头部信息
     * @param pageVO  接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<PageData<MemberRecordRectifyVO>> pageMemberRecordRectify(HttpHeaders headers, ValidateIdPageVO pageVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.pageMemberRecordRectify(loginUser, pageVO);
    }

    /**
     * 会员详情 - 会员等级信息
     *
     * @param headers    Http头部信息
     * @param idVO 接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberValidateDetailLevelVO> getMemberDetailLevel(HttpHeaders headers, ValidateIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getMemberDetailLevel(loginUser, idVO.getValidateId());
    }

    /**
     * 会员详情 - 会员等级信息 - 分页查询交易分获取记录
     *
     * @param headers Http头部信息
     * @param pageVO  接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<PageData<MemberDetailLevelHistoryVO>> pageMemberLevelDetailHistory(HttpHeaders headers, ValidateIdPageVO pageVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.pageMemberLevelDetailHistory(loginUser, pageVO, ServiceConfig.DEFAULT_DATETIME_FORMATTER);
    }

    /**
     * 会员详情 - 会员权益信息
     *
     * @param headers    Http头部信息
     * @param idVO 接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberDetailRightVO> getMemberDetailRight(HttpHeaders headers, ValidateIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getMemberDetailRight(loginUser, idVO.getValidateId());
    }

    /**
     * 会员详情 - 会员权益信息 - 分页查询会员权益获取记录
     *
     * @param headers Http头部信息
     * @param pageVO  接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<PageData<MemberDetailRightHistoryVO>> pageMemberDetailRightHistory(HttpHeaders headers, ValidateIdPageVO pageVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.pageMemberDetailRightHistory(loginUser, pageVO, ServiceConfig.DEFAULT_DATETIME_FORMATTER);
    }

    /**
     * 会员详情 - 会员权益信息 - 分页查询会员权益使用记录
     *
     * @param headers Http头部信息
     * @param pageVO  接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<PageData<MemberDetailRightSpendHistoryVO>> pageMemberDetailRightSpendHistory(HttpHeaders headers, ValidateIdPageVO pageVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.pageMemberDetailRightSpendHistory(loginUser, pageVO, ServiceConfig.DEFAULT_DATETIME_FORMATTER);
    }

    /**
     * 会员详情 - 会员信用信息
     *
     * @param headers    Http头部信息
     * @param idVO 接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberDetailCreditVO> getMemberDetailCredit(HttpHeaders headers, ValidateIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getMemberDetailCredit(loginUser, idVO.getValidateId());
    }

    /**
     * 会员详情 - 会员信用信息 - 交易评价汇总
     *
     * @param headers    Http头部信息
     * @param idVO 接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberDetailCreditCommentSummaryVO> getMemberDetailCreditTradeCommentSummary(HttpHeaders headers, ValidateIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getMemberDetailCreditTradeCommentSummary(loginUser, idVO.getValidateId());
    }

    /**
     * 会员详情 - 会员信用 - 分页查询交易评论历史记录
     *
     * @param headers Http头部信息
     * @param pageVO  接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<PageData<MemberDetailCreditTradeHistoryVO>> pageMemberDetailCreditTradeCommentHistory(HttpHeaders headers, MemberDetailCreditHistoryPageVO pageVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.pageMemberDetailCreditTradeCommentHistory(loginUser, pageVO);
    }

    /**
     * 会员详情 - 会员信用信息 - 售后评价汇总
     *
     * @param headers    Http头部信息
     * @param idVO 接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberDetailCreditCommentSummaryVO> getMemberDetailCreditAfterSaleCommentSummary(HttpHeaders headers, ValidateIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getMemberDetailCreditAfterSaleCommentSummary(loginUser, idVO.getValidateId());
    }

    /**
     * 会员详情 - 会员信用 - 分页查询售后评论历史记录
     *
     * @param headers Http头部信息
     * @param pageVO  接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<PageData<MemberDetailCreditAfterSaleHistoryVO>> pageMemberDetailCreditAfterSaleCommentHistory(HttpHeaders headers, MemberDetailCreditHistoryPageVO pageVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.pageMemberDetailCreditAfterSaleCommentHistory(loginUser, pageVO);
    }

    /**
     * 会员详情 - 会员信用 - 投诉汇总
     *
     * @param headers    Http头部信息
     * @param idVO 接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberDetailCreditComplainSummaryVO> getMemberDetailCreditComplainSummary(HttpHeaders headers, ValidateIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getMemberDetailCreditComplainSummary(loginUser, idVO.getValidateId());
    }

    /**
     * 会员详情 - 会员信用 - 分页查询投诉历史记录
     *
     * @param headers Http头部信息
     * @param pageVO  接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<PageData<MemberCreditComplaintPageQueryVO>> pageMemberDetailCreditComplainHistory(HttpHeaders headers, ValidateIdPageVO pageVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.pageMemberDetailCreditComplainHistory(loginUser, pageVO);
    }

    /**
     * “增加会员角色”功能，查询上级会员列表
     *
     * @param headers Http头部信息
     * @return 查询结果
     */
    @Override
    public Wrapper<UpperMemberShowVO> getUpperMemberInfo(HttpHeaders headers) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        Wrapper<Boolean> wrapper = siteService.isEnableMultiTenancy(headers);
        return baseMemberInfoService.getUpperMemberInfo(loginUser.getMemberId(), loginUser.getMemberRoleId(), wrapper.getData());
    }

    /**
     * “增加会员角色”功能，查询会员类型列表
     *
     * @param headers  Http头部信息
     * @param memberId 会员id
     * @return 查询结果
     */
    @Override
    public Wrapper<List<MemberTypeIdAndNameVO>> getMemberTypeList(HttpHeaders headers, Long memberId) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        if (memberId == null) {
            Wrapper<Boolean> wrapper = siteService.isEnableMultiTenancy(headers);
            if (!wrapper.getData()) {//如果没有开启saas多租户部署
                return baseMemberInfoService.getMemberTypeList(loginUser.getMemberType());
            } else {//如果开启saas多租户部署
                //按照指定会员id取当前会员配置的适用类型
                return baseMemberInfoService.getMemberTypeList(loginUser.getMemberType(), Optional.ofNullable(roleRuleService.memberRoles(loginUser.getMemberId())).map(Wrapper::getData).orElse(new ArrayList<>()));
            }
        }
        //如果有传会员id，则按照指定会员id取适用类型
        return baseMemberInfoService.getMemberTypeList(loginUser.getMemberType(), Optional.ofNullable(roleRuleService.subMemberRoles(memberId)).map(Wrapper::getData).orElse(new ArrayList<>()));
    }

    /**
     * “增加会员角色”功能，根据会员类型Id查询角色列表，以及勾选的角色Id列表
     *
     * @param headers Http头部信息
     * @param idVO    接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberInfoRoleListVO> getRoleListByMemberType(HttpHeaders headers, MemberTypeIdVO idVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        if (idVO.getMemberId() == null) {
            Wrapper<Boolean> wrapper = siteService.isEnableMultiTenancy(headers);
            if (!wrapper.getData()) {//如果没有开启saas多租户部署
                return baseMemberInfoService.getRoleListByMemberType(loginUser.getMemberId(), idVO);
            } else {//如果开启saas多租户部署
                //取当前会员配置的适用角色
                return baseMemberInfoService.getRoleListByMemberType(loginUser.getMemberId(), idVO, Optional.ofNullable(roleRuleService.memberRoles(loginUser.getMemberId())).map(Wrapper::getData).orElse(new ArrayList<>()));
            }
        }
        //如果有传会员id，则按照指定会员id取适用角色
        return baseMemberInfoService.getRoleListByMemberType(loginUser.getMemberId(), idVO, Optional.ofNullable(roleRuleService.subMemberRoles(idVO.getMemberId())).map(Wrapper::getData).orElse(new ArrayList<>()));
    }

    /**
     * “增加会员角色”功能，会获取员注册资料信息
     *
     * @param headers Http头部信息
     * @param roleIdVO 接口参数
     * @return 查询结果
     */
    @Override
    public Wrapper<MemberInfoUpdateDetailByRoleVO> getMemberRegisterDetailAfterAddRole(HttpHeaders headers, RoleIdVO roleIdVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.getMemberRegisterDetailAfterAddRole(loginUser.getMemberId(), loginUser.getMemberRoleId(), roleIdVO);
    }

    /**
     * “增加会员角色”功能，提交注册资料并新增角色
     *
     * @param headers   Http头部信息
     * @param addRoleVO 接口参数
     * @return 新增结果
     */
    @Override
    public Wrapper<Void> addMemberRole(HttpHeaders headers, MemberInfoAddRoleVO addRoleVO) {
        UserLoginCacheDTO loginUser = memberCacheService.needLoginFromBusinessPlatform(headers);
        return baseMemberInfoService.addMemberRole(loginUser.getMemberId(), loginUser.getMemberRoleId(), addRoleVO);
    }
}
